package com.toobigtofail.palette
{
	import com.toobigtofail.placeholders.Placeholder;
	import com.toobigtofail.placeholders.PlankPlaceholder;
	import com.toobigtofail.placeholders.WedgePlaceholder;
	
	import fabulaexmachina.box2fp.graphics.SuperGraphiclist;
	
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.utils.Input;
	
	public class Tray extends Entity
	{
		[Embed(source="/assets/gfx/tray_handle.png")] private static const TRAY_HANDLE_PNG:Class;
		private var _handle:Entity;
		private var _isOpen:Boolean = false;
		private var _items:Dictionary = new Dictionary;
		private var _selectedItemType:String = null;
		
		public function Tray(align:String="right")
		{
			var handleImage:Image = new Image(TRAY_HANDLE_PNG);
			
			width = 200;
			height = FP.screen.height;
			
			y = 0;
			if (align == "right") {
				x = FP.screen.width - handleImage.width;
			}
			_handle = new Entity(x, y, handleImage);
			_handle.width = handleImage.width;
			_handle.height = handleImage.height;
			
			//addItem(new PlankPlaceholder(0,0),2);
			//addItem(new WedgePlaceholder(0,0),1);
		}
		
		public function addItem(item:Placeholder, num:uint=1):void {
			if (!_items[getQualifiedClassName(item)]) {
				item.locked = true;
				_items[getQualifiedClassName(item)] = {item: item, count: num};
			}
			else {
				_items[getQualifiedClassName(item)].count += num;
			}
		}
		
		override public function added():void {
			super.added();
			
			world.add(_handle);
			
			var y:int = this.y + 50;
			for each (var key:String in getOrderedItemKeys()) {
				var item:Placeholder = _items[key].item as Placeholder;
				item.x = this.x + _handle.width + 30;
				item.y = y;
				world.add(item);
				
				y += item.height;
			}
		}
		
		public function remove():void {
			if (world) {
				world.remove(_handle);
				for each (var item:Object in _items) {
					world.remove(item.item);
				}
				world.remove(this);
			}
		}			
		
		override public function render():void {
			super.render();
			
			for each (var key:String in getOrderedItemKeys()) {
				var item:Object = _items[key];
				var p:Placeholder = item.item as Placeholder;
				Draw.graphic(new Text(item.count), p.x - 10, p.y + p.halfHeight);
			}
			
			Draw.rect(x+_handle.width, 0, width-_handle.width, _handle.height, 0xFFFFFF, 0.1);
		}
		
		override public function update():void {
			super.update();
		} 
		
		public function click(mX:Number, mY:Number):Boolean {
			var isClicked:Boolean = false;
			if (_handle.collidePoint(_handle.x, _handle.y, mX, mY)) {
				if (_isOpen) {
					close();
				}
				else {
					open();
				}
				isClicked = true;
			}
			else {
				for (var itemType:String in _items) {
					var p:Placeholder = _items[itemType].item as Placeholder;
					if (p.collidePoint(p.x, p.y, mX, mY)) {
						_selectedItemType = itemType;
						isClicked = true;
						break;
					}
				}
				if (!isClicked && collidePoint(_handle.width+x, y, mX, mY)) {
					isClicked = true;
				}
			}
			if (!isClicked) {
				_selectedItemType = null;
			}
			return isClicked;
		}
		
		public function open():void {
			x = FP.screen.width-width;
			_handle.x = x;
			moveItems();
			_isOpen = true;
		}
		
		public function close():void {
			x = FP.screen.width-_handle.width;
			_handle.x = x;
			moveItems();
			_isOpen = false;
		}
		
		private function moveItems():void {
			for each (var item:Object in _items) {
				(item.item as Placeholder).x = this.x + _handle.width + 20;
			}
		}
		
		private function getOrderedItemKeys():Array {
			var keys:Array = new Array;
			
			for (var key:String in _items) {
				keys.push(key);
			}
			keys.sort();
			
			return keys;
		}
		
		public function hasSelectedItem():Boolean { return _selectedItemType != null; }
		
		public function get isOpen():Boolean { return _isOpen; }
		public function get selectedItem():Placeholder {
			if (_selectedItemType) {
				var item:Object = _items[_selectedItemType];
				if (item) {
					item.count--;
					var p:Placeholder = item.item as Placeholder;
					if (item.count <= 0) {
						world.remove(item.item);
						delete _items[_selectedItemType];
					}
					var ClassRef:Class = getDefinitionByName(_selectedItemType) as Class;
					_selectedItemType = null;
					return (new ClassRef(p.x, p.y) as Placeholder);
				}
				
			}
			return null;
		}
	}
}